package com.quiz.bis.controller;


import com.quiz.bis.domain.bo.BisManualPaperBo;
import com.quiz.bis.domain.vo.BisPaperQuestionDetailVo;
import com.quiz.bis.domain.vo.BisPapersDetailVo;
import com.quiz.bis.service.IBisPaperService;
import com.quiz.common.core.domain.R;
import com.quiz.common.exception.ServiceException;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 试卷管理模块
 * @author ahuan
 */
@RestController
@Tag(name = "试卷管理")
@RequiredArgsConstructor
@RequestMapping("/paper")
public class BisPaperController {

    private final IBisPaperService bisPaperService;


    /**
     * 手动组卷
     * @author ahuan
     * @return {@link R}<{@link Long}> 试卷ID
     */
    @PostMapping("/manual")
    public R<Long> manualPaper(@RequestBody BisManualPaperBo bo) {
        return R.ok(bisPaperService.manualPaper(bo));
    }



    /**
     * 获取试卷预览详情
     * @author ahuan
     * @return {@link R}<{@link BisPapersDetailVo}> 试卷ID
     */
    @GetMapping("/preview/detail/{paperId}")
    public R<BisPapersDetailVo> getPaperDetail(@PathVariable Long paperId) {
        return R.ok(bisPaperService.getPaperDetail(paperId));
    }

    /**
     * 获取试卷编辑详情
     *
     * 编辑时回显的详情信息
     * @author ahuan
     * @return {@link R}<{@link BisPapersDetailVo}> 试卷ID
     */
    @GetMapping("/edit/detail/{paperId}")
    public R<BisPaperQuestionDetailVo> getPaperEditDetail(@PathVariable Long paperId) {
        return R.ok(bisPaperService.getPaperEditDetail(paperId));
    }

    /**
     * 修改试卷
     * @author ahuan
     * @return {@link R}<{@link Long}> 试卷ID
     */
    @PostMapping("/edit")
    public R<Long> editPaper(@RequestBody BisManualPaperBo bo) {
        return R.ok(bisPaperService.editPaper(bo));
    }

    /**
     * 删除试卷
     * @author ahuan
     * @return {@link R}<{@link Boolean}>
     */
    @DeleteMapping("/delete")
    public R<Boolean> deletePaperQuestion(@RequestBody List<Long> quesIds) {
        return R.ok(bisPaperService.deletePaperQuestion(quesIds));
    }

    /**
     * 发布试卷
     * @author ahuan
     * @return {@link R}<{@link Boolean}>
     */
    @PutMapping("/publish/{paperId}")
    public R<Boolean> publishPaper(@PathVariable Long paperId) {
        return R.ok(bisPaperService.publishPaper(paperId));
    }

    /**
     * 撤回试卷
     * @author ahuan
     * @return {@link R}<{@link Boolean}>
     */
    @PutMapping("/recall/{paperId}")
    public R<Boolean> recallPaper(@PathVariable Long paperId) {
        return R.ok(bisPaperService.recallPaper(paperId));
    }

    /**
     * 归档试卷
     * @author ahuan
     * @return {@link R}<{@link Boolean}>
     */
    @PutMapping("/archive/{paperId}")
    public R<Boolean> archivePaper(@PathVariable Long paperId) {
        return R.ok(bisPaperService.archivePaper(paperId));
    }


    /**
     * 导出试卷
     * @param paperId 试卷Id
     * @param type pdf or word
     * @return
     */
    @GetMapping("/export/{paperId}/{type}")
    public void exportPaper(@PathVariable Long paperId,
                               @PathVariable String type,
                               HttpServletResponse response) {
        switch (type){
            case "pdf":
                bisPaperService.exportPaper(paperId,response);
                break;
            case "word":
                bisPaperService.exportWord(paperId,response);
                break;
            default:
                throw new ServiceException("目前系统不支持导出此类型文件!");
        }
    }




}
